package com.net.oil.order.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.order.ReserveDetailActivity;
import com.net.oil.order.bean.ReserveBeanV2;
import com.net.oil.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2018/4/25 0025:下午 21:42
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class ReserveRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 0;  //底部FootView
    private static final int TYPE_ITEM = 1;  //普通item

    private int load_more_status = 10;      //上拉加载更多状态-默认为10

    private Context context;
    private LayoutInflater inflater;

    private List<ReserveBeanV2.ListBean> reserveBeans;

    public ReserveRecyclerViewAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        reserveBeans = new ArrayList<>();
    }

    public void updateData(List<ReserveBeanV2.ListBean> reserveBeans) {
        this.reserveBeans.clear();
        this.reserveBeans.addAll(reserveBeans);
        notifyDataSetChanged();
    }

    public void loadMore(List<ReserveBeanV2.ListBean> reserveBeans){
        this.reserveBeans.addAll(reserveBeans);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM:
                view = inflater.inflate(R.layout.reserve_recycler_view_item, parent, false);
                return new ViewHolder(view);
            case TYPE_FOOTER:
                view = inflater.inflate(R.layout.recycler_view_foot_item, parent, false);
                return new FootViewHolder(view);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            itemDataBind(viewHolder, position);
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footDataBind(footViewHolder);
        }
    }

    private void itemDataBind(ViewHolder viewHolder, int position) {
        final ReserveBeanV2.ListBean reserveBean = reserveBeans.get(position);
        if (reserveBean != null) {
//            holder.reserveTimeTv.setText(DateFormat.formatDateTime(reserveBean.getCreateTime(), DateFormat.FormatFlag.YDM));
            if (!TextUtils.isEmpty(reserveBean.getAppointmentTime())) {
                viewHolder.reserveTimeTv.setText(reserveBean.getAppointmentTime());
            }
            if (!StringUtil.isEmpty(reserveBean.getBusinessName())) {
                viewHolder.reserveShopTv.setText(reserveBean.getBusinessName());
            }

            if (!StringUtil.isEmpty(reserveBean.getCarModel())) {
                viewHolder.carInfoTv.setText(reserveBean.getCarModel());
            }

            /*
             * status状态代表预约状态（expireStatus为0的情况下，1：已预约 0：用户取消预约 2:已到店（完成），expireStatus为1的情况下，就是已过期)
             */
            if (reserveBean.getExpireStatus() == 0) {
                if (reserveBean.getStatus() == Constant.RESERVE_STATUS_SUCCESS) {
                    viewHolder.reserveStatusTv.setText("已预约");
                } else if (reserveBean.getStatus() == Constant.RESERVE_STATUS_CANCEL) {
                    viewHolder.reserveStatusTv.setText("已取消");
                } else if (reserveBean.getStatus() == Constant.RESERVE_STATUS_FINSH) {
                    viewHolder.reserveStatusTv.setText("已完成");
                }
            } else if (reserveBean.getExpireStatus() == 1) {
                viewHolder.reserveStatusTv.setText("已过期");
            }

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ReserveDetailActivity.class);
                    intent.putExtra(Constant.INTENT_RESERVE_ID_KEY, reserveBean.getId());
                    context.startActivity(intent);
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return reserveBeans == null ? 0 : reserveBeans.size() + 1;
    }

    /**
     * 更改滑动的状态
     *
     * @param status 上拉加载更多 PULL_LOAD_MORE = 10; 正在加载中 LOADING_MORE = 11;
     *               没有更多 LOADING_NONE = 12; 上拉加载更多状态-默认为10
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }

    /**
     * 绑定加载更多项的数据
     *
     * @param footViewHolder holder
     */
    private void footDataBind(FootViewHolder footViewHolder) {
        switch (load_more_status) {
            case Constant.PULL_LOAD_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("上拉加载更多...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("正在加载更多数据...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.VISIBLE);
                break;
            case Constant.LOADING_NONE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("没有更多了");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_FAIL:
                footViewHolder.more.setText("加载失败");
                footViewHolder.more.setEnabled(true);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.reserve_status_tv)
        TextView reserveStatusTv;
        @BindView(R.id.reserve_time_tv)
        TextView reserveTimeTv;
        @BindView(R.id.car_info_tv)
        TextView carInfoTv;
        @BindView(R.id.reserve_shop_tv)
        TextView reserveShopTv;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.progress_bar)
        ProgressBar progressBar;
        @BindView(R.id.more)
        TextView more;

        FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
